package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.logic.bussiness.player.UserBagBussiness;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.shuihu.ShuiHuMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

public class StrongholdItemRecycleStrategy extends AbstractItemRecycleStrategy {
    private static Map<Integer, Integer> recycleItemMap = new HashMap<>();

    static {
        recycleItemMap.put(eItemId.STRONGHOLD_GONGPIN.getValue(), 1);
        recycleItemMap.put(eItemId.STRONGHOLD_SHOVEL_ITEM.getValue(), 4);
        recycleItemMap.put(eItemId.STRONGHOLD_CHINA.getValue(), 1);
        recycleItemMap.put(eItemId.STRONGHOLD_TEA.getValue(), 1);
        recycleItemMap.put(eItemId.STRONGHOLD_SILK.getValue(), 1);
        recycleItemMap.put(eItemId.STRONGHOLD_MAMMON_QIANKUNYIZHI.getValue(), 2);
        recycleItemMap.put(eItemId.STRONGHOLD_TRADE_ITEM_1.getValue(), 3);
        recycleItemMap.put(eItemId.STRONGHOLD_TRADE_ITEM_2.getValue(), 3);
        recycleItemMap.put(eItemId.STRONGHOLD_TRADE_ITEM_3.getValue(), 3);
        recycleItemMap.put(eItemId.STRONGHOLD_TRADE_ITEM_4.getValue(), 3);
        recycleItemMap.put(eItemId.STRONGHOLD_TRADE_ITEM_5.getValue(), 3);
        recycleItemMap.put(eItemId.STRONGHOLD_TRADE_ITEM_6.getValue(), 3);
        recycleItemMap.put(eItemId.STRONGHOLD_TRADE_ITEM_7.getValue(), 3);
        recycleItemMap.put(eItemId.STRONGHOLD_TRADE_ITEM_8.getValue(), 3);
    }


    public int getType(int itemId) {
        return recycleItemMap.get(itemId);
    }

    public String getTitleKey(int type) {
        if (type == 1) {
            return MailManager.STRONGHOLD_RETURN_ITEM_TITLE;
        }
        if (type == 2) {
            return MailManager.STRONGHOLD_RETURN_MAMMON_ITEM_TITLE;
        }
        return "";
    }

    public String getContentKey(int type) {
        if (type == 1) {
            return MailManager.STRONGHOLD_RETURN_ITEM_CONTENT;
        }
        if (type == 2) {
            return MailManager.STRONGHOLD_RETURN_MAMMON_ITEM_CONTENT;
        }
        return "";
    }

    private void sendEmail(long userId, String titleKey, String contentKey, Property reward, Map<String, Long> returnItemMap) {
        String language = UserMgr.getLanguage(userId);
        StringBuilder returnStr = new StringBuilder();
        for (Map.Entry<String, Long> entry : returnItemMap.entrySet()) {
            String key = entry.getKey();
            Long value = entry.getValue();
            returnStr.append(ServerLanguageMgr.getContent(key, language)).append("X").append(value).append(", ");
        }
        returnStr.deleteCharAt(returnStr.length() - 1);
        String title = MultipleLanguageMgr.getContent(titleKey, language);
        String content = StringUtils.format(MultipleLanguageMgr.getContent(contentKey, language),
                returnStr.toString());
        MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), content, title);
    }

    @Override
    public void markNeedRecycleItem() {
        ActivityInfo openActivityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (openActivityInfo != null) {
            for (Map.Entry<Integer, Integer> entry : recycleItemMap.entrySet()) {
                Integer itemId = entry.getKey();
                Integer type = entry.getValue();
                if (type == 1 || type == 2) {
                    //领奖期以后回收的：  捐献材料 财神助力  皇家贡品
                    long recycleTime = openActivityInfo.getEndTime() * 1000L + 10 * 60 * 1000;
                    ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, recycleTime);
                } else {
                    //活动入口不见后才回收的  通商凭证 沙漠宝库铲子
                    long recycleTime = openActivityInfo.getEndShowTime() * 1000L + 10 * 60 * 1000;
                    ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, recycleTime);
                }
            }
        }
    }

    @Override
    public boolean isActivityRunning() {
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        return ActivityMgr.activityInTime(activityInfo);
    }

    @Override
    protected void initActivityType() {
        this.activityType = eActivityType.StrongholdWarActivity.getValue();
    }

    @Override
    public void doRecycle(long userId, ActivityItemRecycleData data) {
        int itemId = data.getItemId();
        int type = getType(itemId);
        Property reward = new Property();
        Map<String, Long> returnItemMap = new HashMap<>();
        UserBagItem bagItem = getUserBagItem(userId, itemId);
        if (bagItem != null && bagItem.getNum() > 0) {
            long num = bagItem.getNum();
            if (removeItem(bagItem)) {
                item2Reward(itemId, num, type, reward, returnItemMap);
            }
        }
        if (returnItemMap.size() > 0 && !reward.isNothing()) {
            sendEmail(userId, getTitleKey(type), getContentKey(type), reward, returnItemMap);
        }
    }


    private void item2Reward(int itemId, long num, int type, Property reward, Map<String, Long> returnItemMap) {
        GoodsInfo goodsInfo = CommonMgr.getGoodsInfo(itemId);
        if (type == 1) {
            for (long i = 0; i < num; i++) {
                String extendParam = goodsInfo.getExtendParam();
                Property property = RandomWeightConfigHelper.getRandomProperty(extendParam);
                reward.addProperty(property);
            }
        } else if (type == 2) {
            for (int i = 0; i < num; i++) {
                Property useReward = StrongholdActivityMgr.getMammonUseReward(itemId);
                reward.addProperty(useReward);
            }
        }
        returnItemMap.put(goodsInfo.getGoodsName(), num);
    }
}
